package com.aptemo.webetek.client.widgets;

import com.google.gwt.dom.client.Document;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.Widget;

public class NumberWidget
		extends Widget implements HasValue<Double>
{

	private NumberFormat	formatter;

	public NumberWidget()
	{
		this(NumberFormat.getDecimalFormat());
	}
	
	public NumberWidget(String pattern)
	{
		this(NumberFormat.getFormat(pattern));
	}


	public NumberWidget(NumberFormat formatter)
	{
		setElement(Document.get().createDivElement());
		this.formatter = formatter;
	}

	
	public void setValue(int value)
	{
		getElement().setInnerText(formatter.format(value));
	}
	
	@Override
	public void setValue(Double value)
	{
		if (value.isNaN())
		{
			getElement().setInnerText(value.toString());
			return;
		}
		getElement().setInnerText(formatter.format(value));
	}

	@Override
	public void setValue(Double value, boolean fireEvents)
	{
		setValue(value);
	}

	@Override
	public Double getValue()
	{
		return formatter.parse(getElement().getInnerText());
	}

	@Override
	public HandlerRegistration addValueChangeHandler(ValueChangeHandler<Double> handler)
	{
		return null;
	}

}
